package com.clas.liu.myapplication;

import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.StrictMode;
import android.util.Log;

import com.clas.liu.myapplication.model.entity.user.UserData;
import com.clas.liu.myapplication.view.util.SaveUtil;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.EaseUI;
import com.hyphenate.easeui.domain.EaseUser;
import com.mob.MobSDK;

import java.util.ArrayList;
import java.util.List;

/**
 * application
 * Created by liu on 2019/4/27.
 */

public class MyApplication extends Application {
    private List<Activity> oList;//用于存放所有启动的Activity的集合

    @Override
    public void onCreate() {
        super.onCreate();
        oList = new ArrayList<>();
        MobSDK.init(this);
//        //解决7.0权限问题
//        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
//        StrictMode.setVmPolicy(builder.build());
//        builder.detectFileUriExposure();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
        EaseUI.getInstance().init(this, null);
        EMClient.getInstance().setDebugMode(true);
        EaseUI.getInstance().setUserProfileProvider(new EaseUI.EaseUserProfileProvider() {
            @Override
            public EaseUser getUser(String username) {
                return getUserById(username);
            }
        });
    }

    private EaseUser getUserById(String username) {
        //获取 EaseUser实例, 这里从内存中读取
        //如果你是从服务器中读读取到的，最好在本地进行缓存
        EaseUser user = null;
        //如果用户是本人，就设置自己的头像
        if (username.equals(EMClient.getInstance().getCurrentUser())) {
            user = new EaseUser(username);
            UserData userData = SaveUtil.<UserData>getFromLocal(getApplicationContext(), "user");
            user.setAvatar((String) userData.getHead());
            user.setNickname(userData.getName());
            return user;
        }
        user = new EaseUser(username);
        String info = SaveUtil.getChatUser(getApplicationContext(), username);
        if (info.split("&").length > 1) {
            user.setAvatar(info.split("&")[1]);
            user.setNickname(username+"-"+info.split("&")[0]);
        }
        Log.i("zcb", "头像：" + user.getAvatar());
        return user;
    }

    /**
     * 添加Activity
     */
    public void addActivity_(Activity activity) {
// 判断当前集合中不存在该Activity
        if (!oList.contains(activity)) {
            oList.add(activity);//把当前Activity添加到集合中
        }
    }

    /**
     * 销毁单个Activity
     */
    public void removeActivity_(Activity activity) {
        if (oList.contains(activity)) {
            oList.remove(activity);//从集合中移除
            activity.finish();//销毁当前Activity
        }
    }

    /**
     * 销毁所有的Activity
     */
    public void removeALLActivity_() {
        //通过循环，把集合中的所有Activity销毁
        for (Activity activity : oList) {
            activity.finish();
        }
    }

}
